package com.busfm.provider;

import com.busfm.R;
import com.busfm.activity.HomeActivity;
import com.busfm.service.IplayServiceStub;
import com.busfm.service.PlayerService;
import com.busfm.util.Constants;
import com.busfm.util.LogUtil;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WidgetProvider extends AppWidgetProvider {
    public static String CLICK_NAME_ACTION = "com.busfm.action.widget.click";
    static RemoteViews rv;
    private IplayServiceStub mServiceStub;
    
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        for (int i = 0; i < appWidgetIds.length; i++) {
            int appWidgetId = appWidgetIds[i];
            // 创建一个Intent来启动 ExampleActivity
            Intent intent = new Intent(context, HomeActivity.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
            //为App Widget 取得布局并且拴一个on-click 监听器到该 button 上
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_index);
            views.setOnClickPendingIntent(R.id.widget_index_iv_tag, pendingIntent);
            // 告诉 AppWidgetManager 在当前App Widget上 执行一次更新
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
    }

    public void updateAppWidget(Context context, AppWidgetManager appWidgeManger, int appWidgetId) {
//        Intent musicIntent = new Intent(context, PlayerService.class);
//        context.bindService(musicIntent, mServiceConnection, Context.BIND_AUTO_CREATE);
        
        rv = new RemoteViews(context.getPackageName(), R.layout.widget_index);
        Intent intentClick = new Intent(CLICK_NAME_ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0);
        rv.setOnClickPendingIntent(R.id.widget_index_btn_play, pendingIntent);
        appWidgeManger.updateAppWidget(appWidgetId, rv);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        if (rv == null) {
            rv = new RemoteViews(context.getPackageName(), R.layout.widget_index);
        }
        if (intent.getAction().equals(CLICK_NAME_ACTION)) {
            Toast.makeText(context, "测试",
                    Toast.LENGTH_LONG).show();
            try {
                if (mServiceStub.isPlaying()) {
                    mServiceStub.pause();
                } else {
                    mServiceStub.play();
                }
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        AppWidgetManager appWidgetManger = AppWidgetManager
                .getInstance(context);
        int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(
                context, WidgetProvider.class));
        appWidgetManger.updateAppWidget(appIds, rv);
    }
    
    private ServiceConnection mServiceConnection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            LogUtil.i(Constants.TAG, "onServiceDisconnected");
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            LogUtil.i(Constants.TAG, "onServiceConnected");
            mServiceStub = IplayServiceStub.Stub.asInterface(service);
        }
    };

}
